home *** CD-ROM | disk | FTP | other *** search
- This file is getopts.def, from which is created getopts.c.
- It implements the builtin "getopts" in Bash.
-
- Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
-
- This file is part of GNU Bash, the Bourne Again SHell.
-
- Bash is free software; you can redistribute it and/or modify it under
- the terms of the GNU General Public License as published by the Free
- Software Foundation; either version 1, or (at your option) any later
- version.
-
- Bash is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- for more details.
-
- You should have received a copy of the GNU General Public License along
- with Bash; see the file COPYING. If not, write to the Free Software
- Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- $PRODUCES getopts.c
-
- $BUILTIN getopts
- $DEPENDS_ON GETOPTS_BUILTIN
- $FUNCTION getopts_builtin
- $SHORT_DOC getopts optstring name [arg]
-
- Getopts is used by shell procedures to parse positional parameters.
-
- OPTSTRING contains the option letters to be recognized; if a letter
- is followed by a colon, the option is expected to have an argument,
- which should be separated from it by white space.
-
- Each time it is invoked, getopts will place the next option in the
- shell variable $name, initializing name if it does not exist, and
- the index of the next argument to be processed into the shell
- variable OPTIND. OPTIND is initialized to 1 each time the shell or
- a shell script is invoked. When an option requires an argument,
- getopts places that argument into the shell variable OPTARG.
-
- getopts reports errors in one of two ways. If the first character
- of OPTSTRING is a colon, getopts uses silent error reporting. In
- this mode, no error messages are printed. If an illegal option is
- seen, getopts places the option character found into OPTARG. If a
- required argument is not found, getopts places a ':' into NAME and
- sets OPTARG to the option character found. If getopts is not in
- silent mode, and an illegal option is seen, getopts places '?' into
- NAME and unsets OPTARG. If a required option is not found, a '?'
- is placed in NAME, OPTARG is unset, and a diagnostic message is
- printed.
-
- If the shell variable OPTERR has the value 0, getopts disables the
- printing of error messages, even if the first character of
- OPTSTRING is not a colon. OPTERR has the value 1 by default.
-
- Getopts normally parses the positional parameters ($0 - $9), but if
- more arguments are given, they are parsed instead.
- $END
-
- #include <stdio.h>
- #include "../shell.h"
- #include "getopt.h"
-
- #ifndef NULL
- #define NULL 0
- #endif
-
- #define G_EOF (-1)
- #define G_ILLEGAL_OPT (-2)
- #define G_ARG_MISSING (-3)
-
- extern char *this_command_name;
-
- /* getopts_reset is magic code for when OPTIND is reset. N is the
- value that has just been assigned to OPTIND. */
- void
- getopts_reset (newind)
- int newind;
- {
- optind = newind;
- }
-
- /* Error handling is now performed as specified by Posix.2, draft 11
- (identical to that of ksh-88). The special handling is enabled if
- the first character of the option string is a colon; this handling
- disables diagnostic messages concerning missing option arguments
- and illegal option characters. The handling is as follows.
-
- ILLEGAL OPTIONS:
- name -> "?"
- if (special_error) then
- OPTARG = option character found
- no error output
- else
- OPTARG unset
- diagnostic message
- fi
-
- MISSING OPTION ARGUMENT;
- if (special_error) then
- name -> ":"
- OPTARG = option character found
- else
- name -> "?"
- OPTARG unset
- diagnostic message
- fi
- */
-
- static int
- dogetopts (argc, argv)
- int argc;
- char **argv;
- {
- int ret, special_error, old_opterr = 0;
- char strval[2];
- char *optstr; /* list of options */
- char *name; /* variable to get flag val */
- char *t;
-
- if (argc < 3)
- {
- builtin_error("3 arguments expected");
- return (EXECUTION_FAILURE);
- }
-
- /* argv[0] is "getopts". */
-
- optstr = argv[1];
- name = argv[2];
- argc -= 2;
- argv += 2;
-
- special_error = optstr[0] == ':';
-
- if (special_error)
- {
- old_opterr = opterr;
- optstr++;
- opterr = 0; /* suppress diagnostic messages */
- }
-
- if (argc > 1)
- {
- t = argv[0];
- argv[0] = dollar_vars[0];
- ret = getopt(argc, argv, optstr);
- argv[0] = t;
- }
- else
- {
- register int i;
-
- for (i = 0; dollar_vars[i]; i++);
- ret = getopt (i, dollar_vars, optstr);
- }
-
- if (special_error)
- opterr = old_opterr;
-
- /* Set the OPTIND variable in any case, to handle "--" skipping. */
- {
- char numval[16];
-
- sprintf (numval, "%d", optind);
- bind_variable ("OPTIND", numval);
- }
-
- /* If an error occurred, decide which one it is and set the return
- code appropriately. In all cases, the option character in error
- is in OPTOPT. If an illegal option was encountered, OPTARG is
- NULL. If a required option argument was missing, OPTARG points
- to a NULL string (that is, optarg[0] == 0). */
- if (ret == '?')
- {
- if (optarg == NULL)
- ret = G_ILLEGAL_OPT;
- else if (optarg[0] == '\0')
- ret = G_ARG_MISSING;
- }
-
- if (ret == G_EOF)
- {
- bind_variable (name, "?");
- return (EXECUTION_FAILURE);
- }
-
- if (ret == G_ILLEGAL_OPT)
- {
- /* Illegal option encountered. */
- strval[0] = '?';
- strval[1] = '\0';
- bind_variable (name, strval);
-
- if (special_error)
- {
- strval[0] = (char) optopt;
- strval[1] = '\0';
- bind_variable ("OPTARG", strval);
- }
- else
- makunbound ("OPTARG", shell_variables);
- return (EXECUTION_SUCCESS);
- }
-
- if (ret == G_ARG_MISSING)
- {
- /* Required argument missing. */
- if (special_error)
- {
- strval[0] = ':';
- strval[1] = '\0';
- bind_variable (name, strval);
-
- strval[0] = (char) optopt;
- strval[1] = '\0';
- bind_variable ("OPTARG", strval);
- }
- else
- {
- strval[0] = '?';
- strval[1] = '\0';
- bind_variable (name, strval);
- makunbound ("OPTARG", shell_variables);
- }
- return (EXECUTION_SUCCESS);
- }
-
- bind_variable ("OPTARG", optarg);
-
- strval[0] = (char) ret;
- strval[1] = '\0';
- bind_variable (name, strval);
-
- return (EXECUTION_SUCCESS);
- }
-
- /* The getopts builtin. Build an argv, and call dogetopts with it. */
- int
- getopts_builtin (list)
- WORD_LIST *list;
- {
- register int i;
- char **av;
- int ac, ret;
- WORD_LIST *t = list;
- static int order_set = 0;
-
- if (!list)
- return EXECUTION_FAILURE;
-
- for (ac = 0; t; t = t->next, ac++);
-
- ac++;
- av = (char **)xmalloc ((1 + ac) * sizeof (char *));
- av[ac] = (char *) NULL;
- av[0] = savestring (this_command_name);
-
- for (t = list, i = 1; t; t = t->next, i++)
- av[i] = savestring (t->word->word);
-
- if (order_set == 0)
- {
- getopt_set_posix_option_order (1);
- order_set++;
- }
-
- ret = dogetopts (ac, av);
- free_array (av);
- return (ret);
- }
-